<?php

class controller_user extends controller_theme
{
	public function __construct()
	{
		parent::__construct();
	}
	
	// 登陆页面
	public function login()
	{
		$my = bone::get_user();
		if(!$my->guest && isset($my->is_admin) && $my->is_admin==1)
		{
			$this->redirect('./?controller=user&task=dashboard');
			exit;
		}

		$template = bone::get_template('user.login', APP_DIR);
		$template->set_title('登陆');

		$template->set_data($this->get_data());
		$template->display();
	}

	// 登陆检查
	public function ajax_login_check()
	{
		$username = post('username');
		$password = post('password');

		if(!$username || !$password)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '用户名或密码不能为空');
			$this->ajax();
		}

		$model = bone::get_model('user', APP_DIR);
		$user = $model->login($username, $password );
		
		if( $user )
		{
			$this->set_ajax('status', 0);
			$this->set_ajax('description', '登陆成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}

	// 登陆后首页
	public function dashboard()
	{
		$template = bone::get_template('user.dashboard', APP_DIR);
		$template->set_data($this->get_data());
		$template->display();
	}


	// 管理用户列表
	public function listing()
	{
		$template = bone::get_template('user.listing', APP_DIR);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function ajax_listing()
	{
		$key = get('key', null);

		$model = bone::get_model('user', APP_DIR);
		$this->set_ajax('total', $model->get_user_total($key));
		$this->ajax();
	}


	public function ajax_listing_data()
	{
		$offset = get('offset', 0, 'int');
		$limit = get('limit', 20, 'int');
		$key = get('key', null);

		$model = bone::get_model('user', APP_DIR);
		$user_list = $model->get_user_list($offset, $limit, $key);
		foreach($user_list as $user)
		{
			$user->register_time = date('Y-m-d H:i', $user->register_time);
			$user->last_visit_time = date('Y-m-d H:i', $user->last_visit_time );
		}

		$this->set_ajax( 'user_list', $user_list );
		$this->ajax();
	}

	// 设置用户为管理员
	public function ajax_set_admin()
	{
		$user_id = get('user_id', 0, 'int');
		$status = get('status', 0, 'int');
		if($user_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{user_id}缺失');
			$this->ajax();
		}

		$model = bone::get_model('user', APP_DIR);

		if(!$model->set_admin($user_id, $status))
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '登陆成功');
		$this->ajax();
	}

	// 屏蔽用户
	public function ajax_set_block()
	{
		$user_id = get('user_id', 0, 'int');
		$block = get('block', 0, 'int');
		if($user_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{user_id}缺失');
			$this->ajax();
		}

		$model = bone::get_model('user', APP_DIR);

		if(!$model->block($user_id, $block))
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '登陆成功');
		$this->ajax();
	}

	// 删除用户
	public function ajax_delete()
	{
		$user_id = get('user_id', 0, 'int');
		if($user_id==0)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数{user_id}缺失');
			$this->ajax();
		}

		if($user_id==1)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '默认管理员不能删除');
			$this->ajax();
		}

		$user = bone::get_table('user');
		$user->delete($user_id);

		$this->set_ajax('status', 0);
		$this->set_ajax('description', '删除成功');
		$this->ajax();
	}

	// 修改用户
	public function edit()
	{
		$user_id = get('user_id', 0, 'int');

		$user = bone::get_table('user');
		if($user_id!=0) $user->load($user_id);

		$template = bone::get_template('user.edit', APP_DIR);
		if($user_id!=0)
			$template->set_title('修改用户资料');
		else
			$template->set_title('添加新用户');
		$template->set('user', $user);
		$template->set_data($this->get_data());
		$template->display();
	}

	// 保存修改用户
	public function edit_save()
	{
		$user_id = $_POST['id'];
		
		$url = './?controller=user&task=edit&user_id='.$user_id;

		if(post('username', '')=='') $this->redirect($url, '用户名不能为空', 'error');
		if(post('email', '')=='') $this->redirect($url, '邮箱不能为空', 'error');
		if(post('password', '')!=post('password2', '')) $this->redirect($url, '两次输入的密码不一致', 'error');
		if($user_id==0 && post('password', '')=='') $this->redirect($url, '请输入密码', 'error');

		$model = bone::get_model('user', APP_DIR);
		$result = $model->edit( post() );
		if($result)
			$this->redirect('./?controller=user&task=edit&user_id='.$result, '保存成功');
		else
			$this->redirect($url, $model->get_error(), 'error');
	}

	// 退出登陆
	public function logout()
	{
		$model = bone::get_model('user', APP_DIR);
		$model->logout();

		$this->redirect('./?controller=user&task=login', '成功退出');
	}

}
